<?php
namespace jy_quanhuo;
defined('BY_JYA') or exit('error');
/*
	工具类
*/
class model extends \plugin{
	public function date($time){
		$res=array(
			'day'=>0,
			'hour'=>0,
			'minute'=>0,
			'second'=>0,
		);		
		if($time<=0){
			return $res;
		}
		$res['day']=intval($time/86400);
		$time=$time-$res['day']*86400;
		$res['hour']=intval($time/3600);
		$time=$time-$res['hour']*3600;
		$res['minute']=intval($time/60);
		$res['second']=$time-$res['minute']*60;
		return $res;
	}
	public function upitemlook($pid,$fromuser=0){
		$islook=pdo_get('jy_merch_game_looklog',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'pid'=>$pid));
		$merchid=pdo_getcolumn('jy_merch_game_item',array('uniacid'=>$_SESSION['uniacid'],'id'=>$pid),'merchid');
		if(empty($islook)){
			$insert=array(
				'uniacid'=>$_SESSION['uniacid'],
				'pid'=>$pid,
				'merchid'=>$merchid,
				'mid'=>$_SESSION['uid'],
				'fromuser'=>$fromuser,
				'createtime'=>TIMESTAMP
			);
			pdo_insert('jy_merch_game_looklog',$insert);
		}else{
			if($islook['createtime']+6*3600<time()){
				pdo_update('jy_merch_game_looklog',array('looks +='=>1,'createtime'=>TIMESTAMP),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'pid'=>$id));
			}else{
				pdo_update('jy_merch_game_looklog',array('createtime'=>TIMESTAMP),array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'pid'=>$id));
			}
		}
		return '';
	}
	public function upitemfx($pid,$fromuser=0){
		$islook=pdo_get('jy_merch_game_fxlog',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'pid'=>$pid));
		$merchid=pdo_getcolumn('jy_merch_game_item',array('uniacid'=>$_SESSION['uniacid'],'id'=>$pid),'merchid');
		if(empty($islook)){
			$insert=array(
				'uniacid'=>$_SESSION['uniacid'],
				'pid'=>$pid,
				'merchid'=>$merchid,
				'mid'=>$_SESSION['uid'],
				'fromuser'=>$fromuser,
				'createtime'=>TIMESTAMP
			);
			pdo_insert('jy_merch_game_fxlog',$insert);
		}
		return '';
	}
	/*
		退款打款
	*/
	public function refund_pay($refund_id,$noexi){
		//执行退款操作
		$refund=$this->pdo_get('feelog',array('id'=>$refund_id));
		if(!$refund){
			return false;
		}
		if($refund['status']!=4){
			if($noexi){
				return false;
			}
			exi('退款单不符合打款要求！','error');
		}
		$refund['paytype']=$paytype?:$refund['paytype'];
		//支付方式为余额支付，退款至虚拟币
		if($refund['paytype']=='credit'){
			$pay_out_id=model('core/member')->add_credit2($refund['mid'],$refund['fee'],'订单退款');
		}elseif(strpos($refund['paytype'],'credit') !== false){
			$pay_out_id=model('core/member')->member_credit($refund['mid'],$refund['paytype'],$refund['fee'],true,'','订单退款');
		}elseif($refund['paytype']=='wechat'){
			$payinfo=pdo_get('core_paylog',array('uniacid'=>$_SESSION['uniacid'],'id'=>$refund['pay_id']));
			if(!$refund['transid']){
				//exi('退款失败，没有微信支付号！','error');
			}
			$payinfo['transid']=$refund['transid'];
			$payinfo['money']=$refund['fee'];
			$payinfo['remark']='订单退款';
			$pay_out_id=model('core/member')->refund_out($refund['mid'],$payinfo,$noexi);
		}else{
			$params=array(
				'money'=>$refund['fee'],
				'remark'=>'订单退款',
			);
			$pay_out_id=model('core/member')->pay_out($refund['mid'],$params,$noexi);
		}
		pdo_update('jy_quanhuo_feelog',array('status'=>'5','pay_out_id'=>$pay_out_id),array('uniacid'=>$_SESSION['uniacid'],'id'=>$refund['id']));
		pdo_update('jy_quanhuo_item_jieli',array('status'=>'5'),array('uniacid'=>$_SESSION['uniacid'],'orderid'=>$refund['id']));
		return true;
	}
	public function msgSecCheck($content){
		$result=model('core/wxapp_api')->msgSecCheck($content);
		return $result;
	}
	public function get_wxappewm($url,$pid){//pid要有唯一性
		if(!$url || !$pid || !$_SESSION['wxapp_id']){
			return false;
		}
		$rootat=ATTACHMENT_TYPE == '0'?ROOT_AT:ATTACHMENT_URL;
		$image=$rootat.'oss/'.$_SESSION['uniacid'].'/images/jy_quanhuo/'.$pid.'.jpg';
		if(!file_exists($image)){
			$wxapp=pdo_get('core_account_wxapp',array('id'=>$_SESSION['wxapp_id'],'uniacid'=>$_SESSION['uniacid']));
			$res=model('core/wxapp')->appcode($url,$wxapp['appid'],$wxapp['appsecret'],200);
			$path='oss/'.$_SESSION['uniacid'].'/images/jy_quanhuo';
			if(!file_exists($rootat.$path) && ATTACHMENT_TYPE == '0'){
					mkdir(ROOT_AT.$path,0700, true);
			}
			if($res){
				$filename=$path.'/'.$pid.'.jpg';
				$qr=cfc('files')->source_save($res,'image','png','二维码',$filename,'',$url);
			}
		}
		$ewm='oss/'.$_SESSION['uniacid'].'/images/jy_quanhuo/'.$pid.'.jpg';
		return $ewm;
	}
}